/*
Abstract:
  Serializing functionality implementation

Last changed:
  $Id: outputter.cpp 82 2009-12-08 20:53:44Z vitamin.caig $

Author:
  (C) Vitamin/CAIG/2001

  This file is the part of Textator project
*/
#include "outputter.h"

#include "../io/streams.h"
#include "../parse/parser.h"

#include <algorithm>

#include <text.h>

namespace
{
  inline EntriesMap::value_type SM2EM(const StringMap::value_type& arg)
  {
    return EntriesMap::value_type(arg.first, Entry(Entry::GLOBAL, Token::TEXT, arg.second));
  }

  class TextOut : public Output::Outputter
  {
  public:
    TextOut()
    {
    }

    virtual void Process(const EntriesMap& input, IO::TargetStream& stream) const
    {
      Parser::Parameters params;
      OStringStream str;
      Parser::Unparse(input, params, str);
      stream.PutString(str.str());
    }

    virtual void Process(const StringMap& input, IO::TargetStream& stream) const
    {
      Parser::Parameters params;
      EntriesMap tmp;
      std::transform(input.begin(), input.end(), std::inserter(tmp, tmp.end()), SM2EM);
      return Process(tmp, stream);
    }
  };
}

namespace Output
{
  Outputter::Ptr CreateTextOutputter()
  {
    return Outputter::Ptr(new TextOut());
  }
}
